package com.yanqu.road.server.manger;

import com.yanqu.road.logic.bussiness.servercenter.ChannelActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.CrossActivityProcessBusiness;
import com.yanqu.road.logic.bussiness.union.activitygroup.UnionActivityGroupBusiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.helper.CrossChannelHelper;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.ConfigHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossActivityProcessMgr extends TempMgr {

    private static Map<Integer, Integer> activityProcessMap = new HashMap<>();
    private static Map<Integer, Map<Integer, Integer>> unionActivityProcessMap = new ConcurrentHashMap<>();

    @Override
    public boolean reloadData() throws Exception {
        // 普通活动
        Map<Integer, Integer> activityMap = CrossActivityProcessBusiness.getAllActivityCrossInfo();
        // 跨地区活动
        Map<Integer, Integer> crossChannelActivityMap = getCrossChannelActivityMap();

        Map<Integer, Integer> allMap = new ConcurrentHashMap<>();
        allMap.putAll(activityMap);
        allMap.putAll(crossChannelActivityMap);
        activityProcessMap = allMap;
        return true;
    }

    public static Integer getActivityCrossId(int activityId) {
        if (!activityProcessMap.containsKey(activityId)) {
            synchronized (activityProcessMap) {
                // 普通活动 读中心库
                if (!activityProcessMap.containsKey(activityId)) {
                    Map<Integer, Integer> map = CrossActivityProcessBusiness.getActivityCrossInfo(activityId);
                    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                        activityProcessMap.put(entry.getKey(), entry.getValue());
                        getLogger().info("put new activity {}, cross {}",entry.getKey(),entry.getValue());
                    }
                }
                // 跨地区活动 读跨地区中心库
                if (!activityProcessMap.containsKey(activityId)) {
                    String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
                    if (!"0".equals(dbParam)) {
                        if (ChannelActivityBussiness.isCrossChannelActivity(dbParam, activityId)) {
                            int cross3Id = (int) CrossChannelHelper.getCross3Id(ConfigHelper.getInt("serverId"));
                            activityProcessMap.put(activityId, cross3Id);
                            getLogger().info("put new activity {}, cross {}, this is cross channel activity", activityId, cross3Id);
                        }
                    }
                }
            }
        }
        return activityProcessMap.get(activityId);
    }

    public static int getUnionActivityCrossId(int activityId, int groupId) {
        // 无需分组
        if (groupId == UnionActivityGroupHelper.NO_NEED_GROUP_ID) {
            return getActivityCrossId(activityId);
        }

        if (!unionActivityProcessMap.containsKey(activityId)) {
            synchronized (unionActivityProcessMap) {
                if (!unionActivityProcessMap.containsKey(activityId)) {
                    // 读库
                    Map<Integer, Integer> map = UnionActivityGroupBusiness.getGroupCrossIdMap(activityId);
                    putUnionActivityProcessMap(activityId, map);
                }
            }
        }
        return unionActivityProcessMap.get(activityId).getOrDefault(groupId, -1);
    }

    private static Map<Integer, Integer> getCrossChannelActivityMap() {
        String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
        if ("0".equals(dbParam)) {
            return new ConcurrentHashMap<>();
        }

        List<Integer> channelActivityId = ChannelActivityBussiness.getNoEndCrossChannelActivityId(dbParam, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
        int cross3Id = (int) CrossChannelHelper.getCross3Id(ConfigHelper.getInt("serverId"));

        Map<Integer, Integer> map = new ConcurrentHashMap<>();
        for (Integer id : channelActivityId) {
            map.put(id, cross3Id);
        }
        return map;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void putUnionActivityProcessMap(int activityId, Map<Integer, Integer> map) {
        synchronized (unionActivityProcessMap) {
            unionActivityProcessMap.put(activityId, map);
            getLogger().debug("put union process map activityId {} size {}.", activityId, map.size());
        }
    }
}
